/**
 * 总览清单卡
 */
var numberFormate = iot.getNumberFormat("0");
var resultMap = {
    'totalName': ["总碳排","总成本","总用水","总用电","总燃气","总冷量","总蒸汽","总空气"],
    'totalNameSql': ["工厂碳排表","工厂成本表","工厂水表","工厂电表","工厂燃气表","工厂冷量表","工厂蒸汽表","工厂压缩空气表"],
    'bicyclelName': ["单车碳排","单车成本","单车用水","单车用电","单车燃气","单车冷量","单车蒸汽","单车空气"],
    'unitName': ["t","元","t","kwh","Nm³","Kcal","t","m³"],
    'targetValueName':["碳排目标值表","成本目标值表","用水目标值表","电力目标值表","燃气目标值表","冷量目标值表","蒸汽目标值表","压缩空气目标值表"],
    'startsWithName':["CO","CS","W","E","H","C","蒸汽目标值表","A"]
};
dev.action = function(resp,param){
    var theTime = param.time;
    var workSpace = param.workSpace;
    var jsData = {
        total : iot.list(),
        bicycle : iot.list()
    }
    var totalList = resultMap['totalName'];
    var totalSqlList = resultMap['totalNameSql'];
    var bicyclelList = resultMap['bicyclelName'];
    var unitList = resultMap['unitName'];
    var targetValueList = resultMap['targetValueName'];
    var startsWithList = resultMap['startsWithName'];
    var db = iot.db();
    var result = db.qry("select *,SUM(dev_win.dvalue) totalSum,dev.name as devName" +
        " from iot_dev_data_win dev_win" +
        " LEFT JOIN iot_dev dev" +
        " on dev_win.dev_id = dev.id," +
        " (select * from energy_struc_prop where energy_struc_id = "+ workSpace +") esp" +
        " where dev_win.dev_id = esp.dev_id" +
        " and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '"+ theTime +"%'" +
        " GROUP BY esp.dev_id");

    var resultYield = db.qry("select SUM(dvalue) totalSum from iot_dev_data_win" +
        " where dev_id = 278 and the_time like '"+ theTime +"%'");
    var vlYield = resultYield.getValue();

    var yieldValue = 0;
    resultYield.forAll(function(item){
        yieldValue = item.totalSum;
    });

    //start KPI
    var sn = "'H001','H002','H003'";
    var targetValueTime = theTime;
    if(theTime && theTime.length() == 10){
        targetValueTime = theTime.substring(0,6);
    }

    var factoryName = db.queryOne("select name from energy_struc where id = " + workSpace);
    var espName = '';
    if(factoryName.name !== "郑州工厂"){
        espName = factoryName.name
    }
    //获取目标值
    var targetValueResult = db.qry("SELECT\n" +
        "dev. NAME,\n" +
        "SUM(win.dvalue) totalSum\n" +
        "FROM\n" +
        "iot_dev_data_win win\n" +
        "LEFT JOIN energy_struc_prop esp ON win.dev_id = esp.dev_id\n" +
        "left JOIN iot_dev dev on win.dev_id = dev.id\n" +
        "WHERE\n" +
        "esp.sn IN ("+ sn +")\n" +
        "AND win.the_time LIKE '"+ targetValueTime +"%'\n" +
        "AND win.data_type = esp. NAME \n" +
        "and esp.name like '%"+ espName +"%' \n" +
        "GROUP BY win.dev_id\n");


    //end KPI

    var timeSub = parseInt(theTime.substring(0,4));
    for(var ix in totalList){
        var targetValue = 0;
        var totalUseNum = 0;
        var unit = null;
        var totalItem = totalSqlList[ix];
        var targetValueItem = targetValueList[ix];
        var startsWithItem = startsWithList[ix];
        result.forAll(function (item) {
            if(item && item.devName && (item.devName === totalItem || item.sn.startsWith(startsWithItem))){
                totalUseNum = item.totalSum;
                unit = item.unit;
            }
        });

        if(ix == 0 && timeSub < 2019){
            result.forAll(function (item) {
                if(item && item.devName && (item.devName === totalSqlList[3] || item.sn.startsWith("E"))){
                    totalUseNum = item.totalSum*0.734;
                }
                if(item && item.devName && (item.devName === totalSqlList[4] || item.sn.startsWith("H"))){
                    totalUseNum = totalUseNum + item.totalSum*2.355;
                }
            });
        }
        if(ix == 1 && timeSub < 2019){
            result.forAll(function (item) {
                if(item && item.devName && (item.devName === totalSqlList[3] || item.sn.startsWith("E"))){
                    totalUseNum = item.totalSum*0.64;
                }
                if(item && item.devName && (item.devName === totalSqlList[3] || item.sn.startsWith("H"))){
                    totalUseNum = totalUseNum + item.totalSum*3.6;
                }
            });
        }


        targetValueResult.forAll(function (item) {
            if(item && item.NAME && item.NAME === targetValueItem){
                targetValue = item.totalSum;

            }
        });

        //计算总KPI
        var totalKPI = 0;
        var totalKPIResult = '+0%';
        if(targetValue && targetValue != 0){
            if(targetValue >= totalUseNum){//如果目标值大于总用值
                totalKPI = totalUseNum / targetValue;
                totalKPIResult = "+"+iot.getPercentage(totalKPI);
            }else{
                totalKPI = (totalUseNum - targetValue)/targetValue;
                totalKPIResult = "-"+iot.getPercentage(totalKPI);
            }
        }
        //计算单车KPI
        var oneKPI = 0;
        var oneKPIResult = '+0%';
        //单车KPI
        var bicycleUseNum = 0;
        var oneTargetValue = 0;
        if(yieldValue && yieldValue != 0){
            oneTargetValue = targetValue/yieldValue;
            bicycleUseNum = totalUseNum/yieldValue;
        }
        if(oneTargetValue && oneTargetValue != 0){
            if(oneTargetValue >= bicycleUseNum){//如果目标值大于总用值
                oneKPI = bicycleUseNum / oneTargetValue;
                oneKPIResult = "+"+iot.getPercentage(oneKPI);
            }else{
                oneKPI = (bicycleUseNum - oneTargetValue)/oneTargetValue;
                oneKPIResult = "-"+iot.getPercentage(oneKPI);
            }
        }


        var totalYield = 0;
        if (yieldValue && yieldValue != 0) {
            totalYield = totalUseNum/yieldValue;
        }


        jsData.total.add({planNum: numberFormate.format(targetValue), unit: unitList[ix], type: totalList[ix], useNum: numberFormate.format(totalUseNum), percent: totalKPIResult});
        jsData.bicycle.add({planNum: numberFormate.format(oneTargetValue), unit: unitList[ix]+"/辆", type: bicyclelList[ix], useNum: numberFormate.format(bicycleUseNum), percent: oneKPIResult});
    }

    resp.setJsData(jsData);
}